class HelpAlertView extends egret.DisplayObjectContainer {

    private _bgMask: egret.Shape; // 背景幕布
    private _contentView: egret.DisplayObjectContainer; // contentView
    private _bg: egret.Bitmap; // 背景
    private _bg2: egret.Bitmap; // 内部背景
    private _sureBtn: TbitMap; // 知道了按钮
    private _titleLabel: BaseTextField; // 标题
    private _subTitleLabel: BaseTextField; // 文本标题
    private _textLabel: BaseTextField; // 文本

    private _contentScroller: egret.ScrollView; // 说明内滚动


    private _text: string; // 帮助文案

    constructor() {
        super();
        this.height = GameData.stageH;
        this.width = GameData.stageW;
        this.touchEnabled = true;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.viewDidLoad, this);
    }

    private viewDidLoad() {
        // 1.幕布
        if (!this.contains(this.bgMask)) this.addChild(this.bgMask);
        // 2.contentView
        if (!this.contains(this.contentView)) this.addChild(this.contentView);
        // 3.bg
        if (!this.contentView.contains(this.bg)) this.contentView.addChild(this.bg);
        // 4.内部背景
        if (!this.contentView.contains(this.bg2)) this.contentView.addChild(this.bg2);
        // 5. 确定按钮
        if (!this.contentView.contains(this.sureBtn)) this.contentView.addChild(this.sureBtn);
        // 6. 大标题
        if (!this.contentView.contains(this.titleLabel)) this.contentView.addChild(this.titleLabel);
        // 7. 副标题
        if (!this.contentView.contains(this.subTitleLabel)) this.contentView.addChild(this.subTitleLabel);
        // 8. 文本滚动容器
        if (!this.contentView.contains(this.contentScroller)) this.contentView.addChild(this.contentScroller);

        // 动画
        this.addAnimation();
    }

    /*********************************************** setter **********************************************************/
    public set text(value: string) {
        this._text = value;
        var s = String(value) || '';
        s = s.split("\\n").join("\n");
        this.textLabel.textFlow = <Array<egret.ITextElement>>[{ "text": s+"\n" }];
    }

    /*********************************************** private **********************************************************/
    private tapSureAction() {
        if (this.parent && this.parent.contains(this)) this.parent.removeChild(this);
    }

    private addAnimation() {
        this.contentView.scaleX = 0;
        this.contentView.scaleY = 0;
        egret.Tween.removeTweens(this.contentView);
        egret.Tween.get(this.contentView).to({ scaleX: 1.0, scaleY: 1.0 }, 500, egret.Ease.backOut);
    }

    /*********************************************** getter **********************************************************/

    private get contentScroller(): egret.ScrollView {
        if (!this._contentScroller) {
            var contentScroller = new egret.ScrollView(this.textLabel);
            contentScroller.x = this.bg2.x + 30;
            contentScroller.width = this.contentView.width - 2 * contentScroller.x;
            contentScroller.y = this.bg2.y + 80;
            contentScroller.height = utils.getMaxY(this.bg2) - contentScroller.y - 40;
            contentScroller.horizontalScrollPolicy = "off";
            this._contentScroller = contentScroller;
        }
        return this._contentScroller;
    }

    private get textLabel(): BaseTextField {
        if (!this._textLabel) {
            var textLabel = new BaseTextField();
            textLabel.textColor = 0x6D310C;
            textLabel.lineSpacing = 15;
            // textLabel.x = this.bg2.x + 30;
            textLabel.width = this.contentView.width - 2 * (this.bg2.x + 30);
            // textLabel.y = this.bg2.y + 70;
            textLabel.y = 5;
            this._textLabel = textLabel;
        }
        return this._textLabel;
    }

    private get subTitleLabel(): BaseTextField {
        if (!this._subTitleLabel) {
            var subTitleLabel = new BaseTextField();
            subTitleLabel.textColor = 0xFFEC79;
            subTitleLabel.stroke = 2;
            subTitleLabel.strokeColor = 0x6E3E0E;
            subTitleLabel.text = '宝箱怪游戏说明';
            subTitleLabel.x = this.bg2.x;
            subTitleLabel.bold = true;
            subTitleLabel.y = this.bg2.y + 10;
            subTitleLabel.size = 35;
            subTitleLabel.width = (this.contentView.width - subTitleLabel.x * 2);
            subTitleLabel.textAlign = egret.HorizontalAlign.CENTER;
            this._subTitleLabel = subTitleLabel;
        }
        return this._subTitleLabel;
    }

    private get titleLabel(): BaseTextField {
        if (!this._titleLabel) {
            var titleLabel = new BaseTextField();
            titleLabel.textColor = 0xFFEC79;
            titleLabel.text = "帮 助";
            titleLabel.stroke = 2;
            titleLabel.width = this.contentView.width;
            titleLabel.y = 50;
            titleLabel.textAlign = egret.HorizontalAlign.CENTER;
            titleLabel.size = 50;
            titleLabel.bold = true;
            titleLabel.strokeColor = 0x6E3E0E;
            this._titleLabel = titleLabel;
        }
        return this._titleLabel;
    }

    private get sureBtn(): TbitMap {
        if (!this._sureBtn) {
            var sureBtn = utils.createBitmapByName('know');
            sureBtn.anchorOffsetX = sureBtn.width / 2.0;
            sureBtn.anchorOffsetY = sureBtn.height / 2.0;
            sureBtn.x = this.contentView.width / 2.0;
            sureBtn.y = this.contentView.height - 100;
            sureBtn.touchAnimation = true;
            sureBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapSureAction, this);
            this._sureBtn = sureBtn;
        }
        return this._sureBtn;
    }

    private get bg2(): egret.Bitmap {
        if (!this._bg2) {
            var bg2 = utils.createBitmapByName('help_bg', { width: this.contentView.width - 2 * 20 });
            bg2.x = 60;
            bg2.y = 150;
            bg2.scale9Grid = new egret.Rectangle(78, 140, 400, 440);
            bg2.height = this.contentView.height - 300;
            bg2.width = (this.contentView.width - 2 * bg2.x);
            this._bg2 = bg2;
        }
        return this._bg2;
    }

    private get bg(): egret.Bitmap {
        if (!this._bg) {
            var bg = utils.createBitmapByName('friend', { width: this.contentView.width });
            bg.scale9Grid = new egret.Rectangle(0, 146, 600, 91);
            bg.height = this.contentView.height;
            this._bg = bg;
        }
        return this._bg;
    }

    private get contentView(): egret.DisplayObjectContainer {
        if (!this._contentView) {
            var contentView = new egret.DisplayObjectContainer();
            contentView.touchEnabled = true;
            contentView.width = this.width - 20;
            contentView.height = 940;
            contentView.anchorOffsetX = contentView.width / 2.0;
            contentView.anchorOffsetY = contentView.height / 2.0;
            contentView.x = this.width / 2.0;
            contentView.y = this.height / 2.0;
            this._contentView = contentView;
        }
        return this._contentView;
    }

    private get bgMask(): egret.Shape {
        if (!this._bgMask) {
            var bgMask = new egret.Shape();
            bgMask.graphics.beginFill(0x000000, 0.7);
            bgMask.graphics.drawRect(0, 0, this.width, this.height);
            bgMask.graphics.endFill();
            bgMask.touchEnabled = true;
            bgMask.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapSureAction, this);
            this._bgMask = bgMask;
        }
        return this._bgMask;
    }


}